/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Order2.java
 *
 * Created on 21-08-2011, 12:37:27
 */
package tuitionmanagement.gui;

import Business.OrderBuss;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tuitionmanagement.DAO.OrderDao;
import tuitionmanagement.entity.OrderEntity;
import tuitionmanagement.entity.Student;

/**
 *
 * @author XUAN
 */
public class Order extends javax.swing.JFrame {

    OrderEntity order = new OrderEntity();
    OrderBuss orbuss = new OrderBuss();
    OrderDao ordao = new OrderDao();
    ArrayList<OrderEntity> lst;
    private Student student = new Student();
    int index;
    Vector vRow = new Vector();
    public int confirm;
    private String lastError;

    /** Creates new form Order2 */
    public Order() {
        initComponents();
        ordao.getcmbOrderId(cmbOrderId, "OrderId", txtInvoiceId.getText());
        loadTableOrder();
//        lst = orbuss.readByAll();
//        loadTableOrderDetail(index);
    }

    private void loadTableOrderList(ArrayList<OrderEntity> lst) {
        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();
            order = lst.get(i);
            v.addElement(order.getOrderId());
            v.addElement(order.getInvoiceId());
            v.addElement(order.getMoney());
            v.addElement(order.getFeeDate());
            v.addElement(order.getRateId());

            vRow.addElement(v);
        }

        Vector vCol = new Vector();
        vCol.addElement("OrderId");
        vCol.addElement("InvoiceId");
        vCol.addElement("Money");
        vCol.addElement("FeeDate");
        vCol.addElement("RateId");

        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tbOrder.setModel(tblModel);
    }

    private void loadTableOrder() {
        vRow.clear();
        int invoiceId = Integer.parseInt(txtInvoiceId.getText());
        lst = orbuss.getTableOrder(invoiceId);

//        ArrayList<OrderEntity> lstOrder = new ArrayList<OrderEntity>();
//        lstOrder = listor;

        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();

            order = lst.get(i);
            v.addElement(order.getOrderId());
            v.addElement(order.getInvoiceId());
            v.addElement(order.getMoney());
            v.addElement(order.getFeeDate());
            v.addElement(order.getRateId());

            vRow.addElement(v);
        }

        Vector vCol = new Vector();
        vCol.addElement("OrderId");
        vCol.addElement("InvoiceId");
        vCol.addElement("Money");
        vCol.addElement("FeeDate");
        vCol.addElement("RateId");

        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tbOrder.setModel(tblModel);
    }

    private void loadTableOrderByOrderId() {
        vRow.clear();
        int invoiceId = Integer.parseInt(txtInvoiceId.getText());
        int orderId = Integer.parseInt(cmbOrderId.getSelectedItem().toString());
        lst = orbuss.getTableOrderByOrderId(invoiceId,orderId);

//        ArrayList<OrderEntity> lstOrder = new ArrayList<OrderEntity>();
//        lstOrder = listor;

        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();

            order = lst.get(i);
            v.addElement(order.getOrderId());
            v.addElement(order.getInvoiceId());
            v.addElement(order.getMoney());
            v.addElement(order.getFeeDate());
            v.addElement(order.getRateId());

            vRow.addElement(v);
        }

        Vector vCol = new Vector();
        vCol.addElement("OrderId");
        vCol.addElement("InvoiceId");
        vCol.addElement("Money");
        vCol.addElement("FeeDate");
        vCol.addElement("RateId");

        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tbOrder.setModel(tblModel);
    }

    private void loadAllOrders() {
        lst.clear();
        lst = orbuss.readByAll();
        loadTableOrderList(lst);
    }

    private void loadTableOrderDetail(int index) {
        ArrayList<OrderEntity> list = orbuss.readByAll();
        for (int i = 0; i < list.size(); i++) {
            if (index != list.get(i).getOrderId()) {
                list.remove(i);
                i--;
            }
        }
        loadAllOrders();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtRate = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtInvoiceId = new javax.swing.JTextField();
        txtMoney = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        cmbOrderId = new javax.swing.JComboBox();
        dcDate = new com.toedter.calendar.JDateChooser();
        jPanel3 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbOrder = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        lbmeassage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Order");
        jPanel1.add(jLabel1, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Money:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel9, gridBagConstraints);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("InvoiceId:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel8, gridBagConstraints);

        txtRate.setMinimumSize(new java.awt.Dimension(150, 20));
        txtRate.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 1, 5);
        jPanel2.add(txtRate, gridBagConstraints);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("OrderId:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel7, gridBagConstraints);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setText("Date:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 1, 5);
        jPanel2.add(jLabel10, gridBagConstraints);

        txtInvoiceId.setEditable(false);
        txtInvoiceId.setText("2");
        txtInvoiceId.setMinimumSize(new java.awt.Dimension(150, 20));
        txtInvoiceId.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtInvoiceId, gridBagConstraints);

        txtMoney.setMinimumSize(new java.awt.Dimension(150, 20));
        txtMoney.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtMoney, gridBagConstraints);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel11.setText("Rate:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 1, 5);
        jPanel2.add(jLabel11, gridBagConstraints);

        cmbOrderId.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbOrderIdItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(cmbOrderId, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(dcDate, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 5, 0);
        getContentPane().add(jPanel2, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(9, 5, 6, 5);
        jPanel3.add(btnAdd, gridBagConstraints);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(9, 5, 6, 5);
        jPanel3.add(btnEdit, gridBagConstraints);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(9, 5, 6, 5);
        jPanel3.add(btnDelete, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0);
        getContentPane().add(jPanel3, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        tbOrder.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "OrderId", "InvoiceId", "Money", "Date", "Rate"
            }
        ));
        tbOrder.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbOrderMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbOrder);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 300;
        gridBagConstraints.ipady = 100;
        jPanel4.add(jScrollPane1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel4, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 20);
        getContentPane().add(jPanel5, gridBagConstraints);

        jPanel6.setLayout(new java.awt.GridBagLayout());
        jPanel6.add(lbmeassage, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(jPanel6, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (dcDate.getDate() == null || txtInvoiceId.getText().trim().equals("") || txtMoney.getText().trim().equals("") || txtRate.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }
        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New FeeLevel", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {

            order.setInvoiceId(Integer.parseInt(txtInvoiceId.getText()));
            order.setMoney(Float.parseFloat(txtMoney.getText()));
            order.setFeeDate(dcDate.getDate());
            order.setRateId(Integer.parseInt(txtRate.getText()));

            if (orbuss.addOrder(order)) {
                JOptionPane.showMessageDialog(this, orbuss.getLastError());

                txtInvoiceId.setText(null);
                txtMoney.setText(null);
                dcDate.setDate(null);
                txtRate.setText(null);

                txtInvoiceId.requestFocus();
                vRow.clear();
                loadAllOrders();
                this.tbOrder.updateUI();
            } else {
                JOptionPane.showMessageDialog(this, orbuss.getLastError());
            }
        } else {

            txtInvoiceId.setText(null);
            txtMoney.setText(null);
            dcDate.setDate(null);
            txtRate.setText(null);

        }

    }//GEN-LAST:event_btnAddActionPerformed

    private void tbOrderMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbOrderMouseClicked
        // TODO add your handling code here:
        int selectedIndex = tbOrder.getSelectedRow();
        if (selectedIndex > -1) {
            // RateEntity rate = lst.get(selectedIndex);
            cmbOrderId.setSelectedItem(lst.get(selectedIndex).getOrderId());
            txtInvoiceId.setText(String.valueOf(lst.get(selectedIndex).getInvoiceId()));
            txtMoney.setText(String.valueOf(lst.get(selectedIndex).getMoney()));
            dcDate.setDate(lst.get(selectedIndex).getFeeDate());
            txtRate.setText(String.valueOf(lst.get(selectedIndex).getRateId()));
        }
    }//GEN-LAST:event_tbOrderMouseClicked

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        int selectedIndex = tbOrder.getSelectedRow();
        OrderEntity or = lst.get(selectedIndex);
        int OrderId = or.getOrderId();
        if (dcDate.getDate() == null || txtInvoiceId.getText().trim().equals("") || txtMoney.getText().trim().equals("") || txtRate.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }
        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update FeeLevel", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {

            or.setInvoiceId(Integer.parseInt(txtInvoiceId.getText()));
            or.setMoney(Float.parseFloat(txtMoney.getText()));
            or.setFeeDate(dcDate.getDate());
            or.setRateId(Integer.parseInt(txtRate.getText()));

            if (orbuss.update(or)) {
                //note
                JOptionPane.showMessageDialog(this, or.getLastError());
                vRow.clear();
                loadAllOrders();
                this.tbOrder.updateUI();
                txtInvoiceId.setText(null);
                txtMoney.setText(null);
                dcDate.setDate(null);
                txtRate.setText(null);

            } else {
                JOptionPane.showMessageDialog(this, or.getLastError());
            }
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        int selectedIndex = tbOrder.getSelectedRow();
        OrderEntity or = lst.get(selectedIndex);
        int OrderId = or.getOrderId();

        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Order", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {
            if (orbuss.delete(OrderId)) {
                JOptionPane.showMessageDialog(this, orbuss.getLastError());
                txtInvoiceId.setText(null);
                txtMoney.setText(null);
                dcDate.setDate(null);
                txtRate.setText(null);
                vRow.clear();
                loadAllOrders();
                this.tbOrder.updateUI();
            } else {
                JOptionPane.showMessageDialog(this, orbuss.getLastError());
            }
        }

    }//GEN-LAST:event_btnDeleteActionPerformed

    private void cmbOrderIdItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbOrderIdItemStateChanged
        // TODO add your handling code here:
        loadTableOrderByOrderId();
    }//GEN-LAST:event_cmbOrderIdItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Order().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JComboBox cmbOrderId;
    private com.toedter.calendar.JDateChooser dcDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbmeassage;
    private javax.swing.JTable tbOrder;
    private javax.swing.JTextField txtInvoiceId;
    private javax.swing.JTextField txtMoney;
    private javax.swing.JTextField txtRate;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the student
     */
    public Student getStudent() {
        return student;
    }

    /**
     * @param student the student to set
     */
    public void setStudent(Student student) {
        this.student = student;
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
